
import {
  userPermission
} from '@/api/user'
const state = {
  roleId: '',
  isShowHam: true, // 控制hamburger是否显示
  permissionRoutes: [],
  routesList: [],
  stageId_global: '',
  data_global: [],
  mapPermissionFun: new Map()
};

const mutations = {
  SET_MENU_LIST: (state, routes) => {
    state.permissionRoutes = routes
  },
  SET_STAGE_ID: (state, stageId_global) => {
    state.stageId_global = stageId_global
  },
  SET_STAGE_DATA: (state, data_global) => {
    state.data_global = data_global
  },
  SET_PERMISSION_FUN: (state, list) => {
    list?.map(item => {
      for (const key in item) {
        state.mapPermissionFun.set(item[key], true)
      }
    })
  },
  // 设置用户角色
  SET_ROLES: (state, role) => {
    state.roleId = role
  },

  // 出现topRouter的时候处理路由
  SET_TOP_ROUTER_LIST: (state, payload) => {
    let home = state.permissionRoutes.filter(item => !item.children || item.children.length === 0);
    if (payload) {
      state.routesList = [...home, ...payload];
    } else {
      state.routesList = [...home];
    }
    if (state.routesList && state.routesList.length > 0) {
      if (state.routesList[0].meta && state.routesList[0].meta.icon && state.routesList[0].meta.icon !== "") {
        state.isShowHam = true;
      } else {
        state.isShowHam = false;
      }
    }
  },
};

const actions = {
  // 获取用户权限
  getUserMenu({ commit }) {
    return new Promise((resolve) => {
      userPermission().then((res) => {
      const _data = res.data || {}
      commit("SET_MENU_LIST", _data?.menuList);
      commit("SET_PERMISSION_FUN", res?.data?.functionList);
      commit("SET_ROLES", _data?.currentUser?.roles[0].id);
      resolve(res)
      });
    });
  },
};
export default {
  state,
  mutations,
  actions,
};
